package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
/**
* 游记评论服务接口实现
*/
@Service
//@Transactional  现在写不能事务, 需要讲完复制集才能使用事务
public class TravelCommentServiceImpl implements ITravelCommentService {

    @Autowired
    private TravelCommentRepository repsitory;

    @Autowired
    private MongoTemplate template;

    @Override
    public void save(TravelComment travelComment) {
        travelComment.setId(null);
        repsitory.save(travelComment);
    }

    @Override
    public void update(TravelComment travelComment) {
        repsitory.save(travelComment);
    }

    @Override
    public void delete(String id) {
        repsitory.deleteById(id);
    }

    @Override
    public TravelComment get(String id) {
        return repsitory.findById(id).orElse(null);
    }

    @Override
    public List<TravelComment> list() {
        return repsitory.findAll();
    }

    @Override
    public Page<TravelComment> query(TravelCommentQuery qo) {
        Query query = new Query();
        Pageable pageable = PageRequest.of(qo.getCurrentPage()- 1,qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template,TravelComment.class,query,pageable);
    }

    @Override
    public void commentAdd(TravelComment travelComment) {
        // 判断评论的类型
        String id = travelComment.getRefComment().getId();
        if (StringUtils.hasLength(id)){     // 如果有长度，评论的评论
            TravelComment comment = this.get(id);
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE_COMMENT);
            travelComment.setRefComment(comment);
        }else{      // 没有长度，普通评论
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE);
        }
        //设置创建时间
        travelComment.setCreateTime(new Date());
        this.save(travelComment);
    }

    @Override
    public List<TravelComment> queryTravelComment(TravelCommentQuery qo) {

        /*List<TravelComment> travelComments = repsitory.findByTravelIdOrderByCreateTimeDesc(qo.getTravelId());*/
        Query query = new Query();
        if (StringUtils.hasLength(qo.getTravelId())){
            query.addCriteria(Criteria.where("travelId").is(qo.getTravelId()));
        }
        query.with(Sort.by(Sort.Order.asc("createTime")));
        List<TravelComment> travelComments = template.find(query, TravelComment.class);
        return travelComments;
    }

    @Override
    public void saveOrUpdate(TravelComment travelComment) {
        if(StringUtils.hasLength(travelComment.getId())){
            //update
            this.update(travelComment);
        }else{
            //save
            this.save(travelComment);
        }
    }
}
